package com.chzc.fm.duotin.ui.mine;


import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.Anchor;
import com.chzc.fm.duotin.bean.mine.SpecialBean;
import com.chzc.fm.duotin.ui.mine.utils.MineUrl;
import com.chzc.fm.duotin.ui.other.BaseActivity;
import com.chzc.fm.duotin.utils.Blur;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.FastBlur;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.NumberUtil;
import com.chzc.fm.duotin.utils.Unicode2CN;
import com.chzc.fm.duotin.widget.SpecialPanel;
import com.chzc.fm.duotin.widget.TitleBar;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class AnchorProfileActivity extends BaseActivity {
    //背景和头像
    private ImageView imageBg, imageAvatar;

    //名字， 关注，收听，喜欢，标签，描述
    private TextView textName, textSub, textFollow, textLike, textTag, textDes;

    private SpannableString spanV, spanSub1, spanSub2;

    //主播的Json数据，个人资料
    private Anchor anchor;

    //主播的内容Bean,专辑
    private List<SpecialBean> specialBeans;

    private LinearLayout llContainer;

    private TitleBar titleBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_anchor_profile;
    }

    @Override
    protected void initViews() {
        titleBar = (TitleBar) findViewById(R.id.title_bar);
        imageBg = (ImageView) findViewById(R.id.iv_profile_bg);
        imageAvatar = (ImageView) findViewById(R.id.iv_profile_avatar);
        textName = (TextView) findViewById(R.id.tv_profile_name);
        textSub = (TextView) findViewById(R.id.tv_profile_sub);
        textFollow = (TextView) findViewById(R.id.tv_profile_follow);
        textLike = (TextView) findViewById(R.id.tv_profile_fans);
        textTag = (TextView) findViewById(R.id.tv_profile_tag);
        textDes = (TextView) findViewById(R.id.tv_profile_des);

        llContainer = (LinearLayout) findViewById(R.id.ll_profile_root);

        spanV = getSpannableImage(R.drawable.ic_vip_view);
        spanSub1 = getSpannableImage(R.drawable.ic_follow_nor);
        spanSub2 = getSpannableImage(R.drawable.ic_follow_active);
    }

    //图片String编辑
    private SpannableString getSpannableImage(int resId) {
        SpannableString spannableText = new SpannableString(" 1 ");
        Drawable d = getResources().getDrawable(resId);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        spannableText.setSpan(imageSpan, 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableText;
    }

    //编辑关注按钮
    private void textSubIsSelected(boolean flag){

        if(!flag){
            textSub.setSelected(true);
//            textSub.setText(spanSub1);
            setDrawableImg(R.drawable.ic_follow_nor);
            textSub.setText("  关注");
        }else{
            textSub.setSelected(false);
//            textSub.setText(spanSub2);
            setDrawableImg(R.drawable.ic_follow_active);
            textSub.setText("已关注");
        }
    }

    /**
     * 设置drawableLeft
     * @param resID
     */
    private void setDrawableImg(int resID){
        Drawable drawable = getResources().getDrawable(resID);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        textSub.setCompoundDrawables(drawable, null, null, null);
        textSub.setCompoundDrawablePadding(5);
    }

    @Override
    protected void initEvents() {
        textSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textSubIsSelected(textSub.isSelected());
            }
        });

        titleBar.setLeftListener(new TitleBar.mTitleBarListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        textDes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(textDes.getMaxLines() == 3) {
                    textDes.setMaxLines(10);
                }else{
                    textDes.setMaxLines(3);
                }
            }
        });
    }


    @Override
    protected void init() {

        titleBar.setTitleBarVisibility(true, "主播电台");
        titleBar.setLeft(true, R.drawable.arrow_back_white);
        titleBar.setRight(true);

        final String id = getIntent().getStringExtra("id");
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return Unicode2CN.unicode2GBK((String) HttpUtil.doGet(MineUrl.getPodcastUrl(id)));
            }
        };
        DuotinTask.IRequestCallback callback = new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {
                try {
                    JSONObject root = new JSONObject(object.toString());
                    String msg = root.getString(GlobalParam.ERROR_MSG);
                    if(GlobalParam.SUCCESS.equals(msg.toLowerCase())){
                        JSONObject data = root.getJSONObject("data");
                        JSONObject podcast = data.getJSONObject("podcast");
                        JSONArray album_list = data.getJSONArray("album_list");
                        anchor = Anchor.objectFromData(podcast.toString());
                        specialBeans = SpecialBean.arraySpecialBeanFromData(album_list.toString());
                        initData();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        };
        DuotinTask task = new DuotinTask(request, callback);
        putAsyncTask(task);
    }

    private void initData() {

        final Bitmap bitmap = ImageLoader.getInstance().loadImageSync(anchor.getImage_url());
        imageAvatar.setImageBitmap(bitmap);

        textName.setText(anchor.getReal_name());

        textFollow.setText("收听：" + NumberUtil.countNumber(anchor.getPlay_num()));
        textLike.setText("粉丝：" + NumberUtil.countNumber(anchor.getFollowed_num()));

        textTag.setText("标签：");
        for (Anchor.TagsBean tag:anchor.getTags()) {
            textTag.append(tag.getName() + " ");
        }
        textDes.setText("简介：" + anchor.getDescribe());

        if(anchor.getIs_followed() == 1){
            textSub.setSelected(true);
            textSubIsSelected(true);
        }else{
            textSubIsSelected(false);
        }
        if(anchor.getIs_v().equals("1")){
            textName.append(spanV);
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap blurMap = Blur.fastblur(getBaseContext(), bitmap, 20);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageBg.setImageBitmap(blurMap);
                    }
                });
            }
        }).start();

        if(null != specialBeans && specialBeans.size() > 0){
            for (SpecialBean special:specialBeans) {
                SpecialPanel panel = new SpecialPanel(this, special);
                llContainer.addView(panel);
            }
        }


    }
}
